package net.wsfcp.android.ui.view.my.fragment;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.view.View;
import android.widget.LinearLayout;

import com.yanzhenjie.recyclerview.swipe.SwipeItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import net.wsfcp.android.R;
import net.wsfcp.android.adapter.AddressAdapter;
import net.wsfcp.android.base.BaseFragment;
import net.wsfcp.android.bean.resultbean.AddressBean;
import net.wsfcp.android.http.mvp.contract.AddressContract;
import net.wsfcp.android.event.AddressEvent;
import net.wsfcp.android.http.mvp.presenter.EditAddressPresenter;
import net.wsfcp.android.ui.widget.RecycleViewDivider;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

//地址列表子页面
public class AddressFragment extends BaseFragment<EditAddressPresenter> implements AddressContract.EditAddressView {
    @BindView(R.id.rv_address)
    SwipeMenuRecyclerView rvAddress;//添加侧滑菜单的列表

    AddressAdapter addressAdapter;
    ArrayList<AddressBean> addressBeans = new ArrayList<AddressBean>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_address;
    }

    @Override
    protected EditAddressPresenter loadPresenter() {
        return new EditAddressPresenter();
    }

    @Override
    protected void initData() {
        mPresenter.getAddressList();

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected void initView() {
        initRecyclerView();//初始化recyclerview
        initSwipeMenu();//添加侧边栏
        initAdapter();

    }

    public void initAdapter() {
        //设置Adapter
        addressAdapter = new AddressAdapter(getContext());
        addressAdapter.setList(addressBeans);
        rvAddress.setAdapter(addressAdapter);
    }


    public void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        //设置布局管理器
        rvAddress.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        //设置分隔线
        rvAddress.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayoutManager.VERTICAL, dp(8), getResources().getColor(R.color.white)));
        //设置增加或删除条目的动画
//        rvDrawer.setItemAnimator( new DefaultItemAnimator());


    }


    public void initSwipeMenu() {
        // 创建菜单：
        SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
                SwipeMenuItem editItem = new SwipeMenuItem(getContext()); // 各种文字和图标属性设置。
                editItem.setImage(R.mipmap.address_icon_08);//图标
                editItem.setBackground(R.color.address_btn_edit);//背景色
                editItem.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
                editItem.setWidth(dp(60));//按钮宽度
                rightMenu.addMenuItem(editItem); // 在Item左侧添加一个菜单。

                if (!addressBeans.get(position).isDefault()) {//选择默认地址按钮,已经是默认地址的不必添加
                    SwipeMenuItem checkItem = new SwipeMenuItem(getContext());
                    checkItem.setImage(R.mipmap.address_icon_09);
                    checkItem.setBackground(R.color.address_btn_check);
                    checkItem.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
                    checkItem.setWidth(dp(60));
                    rightMenu.addMenuItem(checkItem);
                }
                SwipeMenuItem deleteItem = new SwipeMenuItem(getContext());
                deleteItem.setImage(R.mipmap.address_icon_10);
                deleteItem.setBackground(R.color.address_btn_delete);
                deleteItem.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
                deleteItem.setWidth(dp(60));
                rightMenu.addMenuItem(deleteItem);

                // 注意：哪边不想要菜单，那么不要添加即可。
//                leftMenu.addMenuItem(Item);
            }
        };
        rvAddress.setSwipeMenuCreator(mSwipeMenuCreator);


        // 菜单点击监听。
        SwipeMenuItemClickListener mMenuItemClickListener = new SwipeMenuItemClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, final int position) {
                // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
                menuBridge.closeMenu();
                // 左侧还是右侧菜单：
                int direction = menuBridge.getDirection();
                // 菜单在Item中的Position：
                int menuPosition = menuBridge.getPosition();
//由于默认地址侧边栏按钮为2个，非默认地址侧边栏按钮为3个，所以要判断下标
                switch (menuPosition) {
                    case 0:
                        EventBus.getDefault().post(new AddressEvent(2, addressBeans.get(position)));
                        break;
                    case 1:
                        if (addressBeans.get(position).isDefault()) {//删除
                            new AlertDialog.Builder(getContext()).setTitle("确定要删除该地址吗?")
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            mPresenter.deleteAddress(addressBeans.get(position).getId());

                                        }
                                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {

                                    dialogInterface.dismiss();
                                }
                            }).create().show();


                        } else {//设置默认地址
                            mPresenter.setDefaultAddress(addressBeans.get(position).getId());
                        }

                        break;
                    case 2://删除
                        new AlertDialog.Builder(getContext()).setTitle("确定要删除该地址吗?")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        mPresenter.deleteAddress(addressBeans.get(position).getId());

                                    }
                                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                dialogInterface.dismiss();
                            }
                        }).create().show();


                        break;
                }

            }
        };
        rvAddress.setSwipeMenuItemClickListener(mMenuItemClickListener);


        //Item点击监听
        rvAddress.setSwipeItemClickListener(new SwipeItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                switch (view.getId()) {


                }
            }
        });
        //设置侧边栏
        rvAddress.setSwipeMenuCreator(mSwipeMenuCreator);

    }


    @Override
    public void getAddressListSuccess(ArrayList<AddressBean> bean) {
        addressBeans.clear();
        addressBeans.addAll(bean);
        addressAdapter.setList(addressBeans);
        addressAdapter.notifyDataSetChanged();

        EventBus.getDefault().post(new AddressEvent(10, addressBeans.size()));

    }

    @Override
    public void getAddressListFail(String failMsg) {
//        toast(failMsg);
        addressBeans.clear();
        addressAdapter.setList(addressBeans);
        addressAdapter.notifyDataSetChanged();
        EventBus.getDefault().post(new AddressEvent(10, 0));
    }

    @Override
    public void setDefaultAddressSuccess() {
        toast("设置成功");
        EventBus.getDefault().post(new AddressEvent(1));
    }

    @Override
    public void setDefaultAddressFail(String failMsg) {
        toast(failMsg);
    }

    @Override
    public void deleteAddressSuccess() {
        toast("删除成功");
        EventBus.getDefault().post(new AddressEvent(1));
    }

    @Override
    public void deleteAddressFail(String failMsg) {
        toast(failMsg);
    }


    //接收订阅的事件
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void login(AddressEvent event) {
        switch (event.getCode()) {
            case 0:   // 删除
                mPresenter.getAddressList();
                break;
            case 1:   // 新增
                mPresenter.getAddressList();
                break;
            case 2:   // 修改
//
                break;

        }

    }

}
